//
//  TDTipsViewController.m
//  TaduFramework
//
//  Created by Xu Nicholas on 11-8-1.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "TDTipsViewController.h"


#if TARGET_OS_IPHONE

#import "ModalViewController.h"
#import "FunctionSet.h"
#import "UIViewController+Ext.h"

static UIView *_mainWindow = nil;
static TDTipsViewController *_tips_view = nil;

@implementation TDTipsViewController

@synthesize textViewForTips, rightButton, leftButton;
@synthesize modal, subView, tipsLabel,tipsLabelt;
@synthesize tipsString;
@synthesize tipsStringt;
@synthesize subframe;
@synthesize activity;
@synthesize edgeInsetsForTipsLabel;
@synthesize backgroundImage;
@synthesize panel;
@synthesize info;
@synthesize delegate;
@synthesize cancelButtonOnCorner = _cancelButtonOnCorner;

+ (void)setMainWindow:(UIView *)mainWindow
{
    @synchronized(self) {
        XRetain(mainWindow);
        SafeRelease(_mainWindow);
        _mainWindow = mainWindow;
    }
}

+ (void)showTipsMessage:(NSString *)message
{
    [self showTipsMessage:message withTitle:nil duration:0.0];
}

+ (void)showTipsMessage:(NSString *)message duration:(NSTimeInterval) durationTime
{
    NSLog(@"main window :%@", _mainWindow);
    [self showTipsMessage:message withTitle:nil duration:durationTime];
}

+ (void)showTipsMessage:(NSString *)message withTitle:(NSString *)title duration:(NSTimeInterval)durationTime
{
    @synchronized(self) {
        TDTipsInfo *info = [TDTipsInfo infoWithTitle:title Message:message TimeInterval:durationTime HiddenActivity:(durationTime != 0.0)];
        info.updateView = NO;
        [_tips_view showViewWithTipsInfo:info];
    }
}

+ (void)initialize
{
    if (!_tips_view) {
        _tips_view = [[TDTipsViewController alloc] initWithNibName:@"TDTipsViewController" bundle:nil];
    }    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        if (modal == nil) {
            modal = [ModalViewController new];
        }
        if (_tips_queue == NULL) {
            _tips_queue = dispatch_queue_create("com.tadu.tips.queue", NULL);
        }
        if (!_tips_operation_queue) {
            _tips_operation_queue = [[NSOperationQueue alloc] init];
        }
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
    VIEW_DID_UNLOAD_FUNCTION();
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    //NSLog(@"%s :%@", __func__, NSStringFromBool([[NSThread currentThread] isMainThread]));
    
    if (!_cancelButtonOnCorner) {
        self.cancelButtonOnCorner = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButtonOnCorner addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside];
        _cancelButtonOnCorner.frame = CGRectMake(0, 0, 12, 12);
        [_cancelButtonOnCorner setBackgroundImage:[UIImage imageNamed:@"弹窗关闭"] forState:UIControlStateNormal];
        [self.subView addSubview:_cancelButtonOnCorner];
    }
    
    self.subView.layer.cornerRadius = 7.0;
    self.subView.layer.masksToBounds = YES;

	self.subView.backgroundColor = [UIColor colorWithRed:0.059 green:0.059 blue:0.059 alpha:0.8];
	self.subView.center = CGPointMake(self.view.frame.size.width / 2.0f, self.view.frame.size.height / 2.0);
	
    CGRect frameForActive = [activity frame];
    frameForActive.size.width = 20;
    frameForActive.size.height = 20;
    activity.frame = frameForActive;
    self.tipsLabel.text = self.tipsString;
    self.tipsLabel.textAlignment = UITextAlignmentCenter;
    self.tipsLabel.numberOfLines = 0;
    self.tipsLabel.lineBreakMode = UILineBreakModeCharacterWrap;
    subframe = self.subView.frame;
    self.edgeInsetsForTipsLabel = UIEdgeInsetsMake(10, 10, 10, 10);
    activity.hidden = YES;

    //NSLog(@"%s  sub :%@; panel :%@", __FUNCTION__, NSStringFromCGRect(self.subView.frame), NSStringFromCGRect(panel.frame));
}

- (void)viewWillAppear:(BOOL)animated
{
    self.view.frame = APP_FRAME;
    panel.backgroundColor = [UIColor clearColor];
    //Re-locate cancelButtonOnCorner.
    self.cancelButtonOnCorner.center = CGPointMake((self.subView.bounds.size.width - self.cancelButtonOnCorner.frame.size.width / 2.0) - 10,
                                                   (self.cancelButtonOnCorner.frame.size.height / 2.0) + 10);
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //NSLog(@"%s  sub :%@; panel :%@", __FUNCTION__, NSStringFromCGRect(self.subView.frame), NSStringFromCGRect(panel.frame));
}

#pragma mark---根据info更改不同状态2

//-(void)differentInfoToView
//{
//    if (tipsLabelt.text!=nil)
//    {
//        self.tipsLabelt.text = nil;
//    }
//    CGPoint beginPoint = {10,10};
//    CGRect panelRect = [panel frame];
//    CGRect frameforTipsLabelt = [self.tipsLabelt frame];
//    CGRect frameforAcitive = [activity frame];//frameforActive ----滚轮的frame
//    CGRect frameforLabel = [tipsLabel frame];
//    int heightAcitiveWithTipLabel = 10;//label与滚轮的间隔
//    /*****tiplabelt**********/
//        CGRect myRect = [tipsLabel frame];
//        self.tipsLabelt = [[UILabel alloc] initWithFrame:myRect];//让两个label大小一样    
//       // self.tipsLabelt.font = [UIFont fontWithName:@"Arial" size:16];
//        self.tipsLabelt.textAlignment = UITextAlignmentCenter;
//        self.tipsLabelt.textColor = [UIColor whiteColor];
//        self.tipsLabelt.backgroundColor = [UIColor clearColor];
//        self.tipsLabelt.numberOfLines = 2;
//        self.tipsLabelt.lineBreakMode = UILineBreakModeCharacterWrap;
//        self.tipsLabelt.font = [UIFont fontWithName:@"Helvetica" size:20];//14
//        self.tipsLabelt.text = info.title;
//        NSLog(@"the title is %@",info.title);
//        frameforTipsLabelt.origin.x = beginPoint.x;
//        frameforTipsLabelt.origin.y = beginPoint.y;
//        frameforTipsLabelt.size.width = panelRect.size.width - 20;
//        CGSize mychangelabelt = [tipsLabelt.text sizeWithFont:[UIFont fontWithName:@"Arial" size:16] constrainedToSize:CGSizeMake(panelRect.size.width-20,2000)lineBreakMode:UILineBreakModeCharacterWrap];
//        frameforTipsLabelt.size.height = mychangelabelt.height;
//        self.tipsLabelt.frame = frameforTipsLabelt;
//        self.tipsLabelt.hidden = NO;
//        [self.panel addSubview:tipsLabelt];
//    /*****activity**********/
//        frameforAcitive.size.width = 20;
//        frameforAcitive.size.height = 20;
//        frameforAcitive.origin.x = (panelRect.size.width - frameforAcitive.size.width)/2;//让active居中在pannel
//    NSLog(@"---%f--",frameforLabel.origin.x);
////        activity.frame = frameforAcitive;
//    /**********tipslabel.frame*****************/
//    CGSize mychangelabel = [tipsLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:14] constrainedToSize:CGSizeMake(panelRect.size.width-20,2000)lineBreakMode:UILineBreakModeCharacterWrap];    //文字动态的高度，固定的宽度
//    frameforLabel.origin.x = beginPoint.x ;//相对panelview的左上顶点的位置
//    frameforLabel.size.width = panelRect.size.width - 20;//tiplabel就要比下层的panel窄20
//    frameforLabel.size.height = mychangelabel.height;//label的高度是文字的动态高度
////    self.tipsLabel.frame = frameforLabel;
//    /**********panel.frame*****************///
//    
//    if (info.title.length == 0)
//    {
//        if (info.hidden == YES)
//        {
//            frameforLabel.origin.y = heightAcitiveWithTipLabel;
//            panelRect.size.height = heightAcitiveWithTipLabel+frameforLabel.size.height+frameforLabel.origin.y;
//            self.tipsLabel.frame = frameforLabel;
//        }
//        else {
//            
//            frameforAcitive.origin.y = heightAcitiveWithTipLabel;
//            frameforLabel.origin.y = frameforAcitive.origin.y + frameforAcitive.size.height+heightAcitiveWithTipLabel;
//            panelRect.size.height = frameforLabel.origin.y +frameforLabel.size.height + heightAcitiveWithTipLabel;
//            self.tipsLabel.frame = frameforLabel;
//             activity.frame = frameforAcitive;
//        }
//    }
//    else {
//        if (info.hidden == YES) {//有标题
//            frameforTipsLabelt.origin.y = heightAcitiveWithTipLabel;
//            frameforLabel.origin.y = frameforTipsLabelt.origin.y + frameforTipsLabelt.size.height+ heightAcitiveWithTipLabel;
//            panelRect.size.height = frameforLabel.origin.y +frameforLabel.size.height + heightAcitiveWithTipLabel;
//            self.tipsLabel.frame = frameforLabel;
//        }
//        else {
//            
//            frameforTipsLabelt.origin.y = heightAcitiveWithTipLabel;
//            frameforAcitive.origin.y = frameforTipsLabelt.origin.y +frameforTipsLabelt.size.height+ heightAcitiveWithTipLabel;
//            frameforLabel.origin.y = frameforAcitive.origin.y+frameforAcitive.size.height + heightAcitiveWithTipLabel;
//            panelRect.size.height = frameforLabel.origin.y + frameforLabel.size.height + heightAcitiveWithTipLabel;
//           // self.tipsLabelt.frame = frameforTipsLabelt;
//             activity.frame = frameforAcitive;
//            self.tipsLabel.frame = frameforLabel;
//
//        }
//    
//    }
//    
//    
//    panel.frame = panelRect;//
//    /**********subview.frame*****************/
//    CGRect subRect = [self.subView frame];//最大的subviewframe
//    CGRect selfRect = [self.view frame];
//    subRect.size.height = panelRect.size.height;//高度和panel一样
//    subRect.origin.y = (selfRect.size.height - subRect.size.height)/2;//顶点坐标在屏幕中心少下一点
//    subView.frame = subRect;
//}

//Todo:需要改名为 updateTipsInfoToView；方法内逻辑需要整理
-(void)differentInfoToView
{
    //Note:avoid view be nil.
    if (!self.view) {
        return;
    }
    //NSLog(@"%s  sub :%@; panel :%@", __FUNCTION__, NSStringFromCGRect(self.subView.frame), NSStringFromCGRect(panel.frame));

    if (tipsLabelt.text!=nil)
    {
        self.tipsLabelt.text = nil;
    }
    CGPoint beginPoint = {5,15};
    CGRect panelRect = [panel frame];
    CGRect frameforTipsLabelt = [self.tipsLabelt frame];
    CGRect frameforAcitive = [activity frame];//frameforActive ----滚轮的frame
    CGRect frameforLabel = [tipsLabel frame];
    int heightAcitiveWithTipLabel = 10;//label与滚轮的间隔
    /*****tiplabelt**********/
    CGRect myRect = [tipsLabel frame];
    self.tipsLabelt = [[UILabel alloc] initWithFrame:myRect];//让两个label大小一样    
    // self.tipsLabelt.font = [UIFont fontWithName:@"Arial" size:16];
    self.tipsLabelt.textAlignment = UITextAlignmentCenter;
    self.tipsLabelt.textColor = [UIColor whiteColor];
    self.tipsLabelt.backgroundColor = [UIColor clearColor];
    self.tipsLabelt.numberOfLines = 2;
    self.tipsLabelt.lineBreakMode = UILineBreakModeCharacterWrap;
    self.tipsLabelt.font = [UIFont fontWithName:@"Helvetica" size:20];//14
    self.tipsLabelt.text = info.title;
    //NSLog(@"the title is %@",info.title);
    frameforTipsLabelt.origin.x = beginPoint.x;
    frameforTipsLabelt.origin.y = beginPoint.y;
    frameforTipsLabelt.size.width = panelRect.size.width - 10;
    CGSize mychangelabelt = [tipsLabelt.text sizeWithFont:[UIFont fontWithName:@"Arial" size:16] constrainedToSize:CGSizeMake(panelRect.size.width-10,2000)lineBreakMode:UILineBreakModeCharacterWrap];
    frameforTipsLabelt.size.height = mychangelabelt.height;
    self.tipsLabelt.frame = frameforTipsLabelt;
    self.tipsLabelt.hidden = NO;
    [self.panel addSubview:tipsLabelt];
    /*****activity**********/
    frameforAcitive.size.width = 20;
    frameforAcitive.size.height = 20;
    frameforAcitive.origin.x = (panelRect.size.width - frameforAcitive.size.width)/2;//让active居中在pannel
    //NSLog(@"---%f--",frameforLabel.origin.x);
    //        activity.frame = frameforAcitive;
    /**********tipslabel.frame*****************/
    CGSize mychangelabel = [tipsLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:14] constrainedToSize:CGSizeMake(panelRect.size.width-20,2000)lineBreakMode:UILineBreakModeCharacterWrap];    //文字动态的高度，固定的宽度
    frameforLabel.origin.x = beginPoint.x ;//相对panelview的左上顶点的位置
    frameforLabel.size.width = panelRect.size.width - 10;//tiplabel就要比下层的panel窄20
    frameforLabel.size.height = mychangelabel.height;//label的高度是文字的动态高度
    //    self.tipsLabel.frame = frameforLabel;
    /**********panel.frame*****************///
    
    if (info.title.length == 0)
    {
        if (info.hidden == YES)
        {
            frameforLabel.origin.y = heightAcitiveWithTipLabel+5;
            panelRect.size.height = heightAcitiveWithTipLabel+5+frameforLabel.size.height+heightAcitiveWithTipLabel+5;
            self.tipsLabel.frame = frameforLabel;
        }
        else {
            
            frameforAcitive.origin.y = heightAcitiveWithTipLabel + 5;
            frameforLabel.origin.y = frameforAcitive.origin.y + frameforAcitive.size.height+heightAcitiveWithTipLabel;
            panelRect.size.height = frameforLabel.origin.y +frameforLabel.size.height + heightAcitiveWithTipLabel+5;
            self.tipsLabel.frame = frameforLabel;
            activity.frame = frameforAcitive;
        }
    }
    else {
        if (info.hidden == YES) {//有标题
            frameforTipsLabelt.origin.y = heightAcitiveWithTipLabel+5;
            frameforLabel.origin.y = frameforTipsLabelt.origin.y + frameforTipsLabelt.size.height+ heightAcitiveWithTipLabel;
            panelRect.size.height = frameforLabel.origin.y +frameforLabel.size.height + heightAcitiveWithTipLabel+5;
            self.tipsLabel.frame = frameforLabel;
        }
        else {
            
            frameforTipsLabelt.origin.y = heightAcitiveWithTipLabel+5;
            frameforAcitive.origin.y = frameforTipsLabelt.origin.y +frameforTipsLabelt.size.height+ heightAcitiveWithTipLabel;
            frameforLabel.origin.y = frameforAcitive.origin.y+frameforAcitive.size.height + heightAcitiveWithTipLabel;
            panelRect.size.height = frameforLabel.origin.y + frameforLabel.size.height + heightAcitiveWithTipLabel+5;
            // self.tipsLabelt.frame = frameforTipsLabelt;
            activity.frame = frameforAcitive;
            self.tipsLabel.frame = frameforLabel;
            
        }
        
    }
    
    panel.frame = panelRect;//
    /**********subview.frame*****************/
    //NSLog(@"%s  sub :%@; panel :%@", __FUNCTION__, NSStringFromCGRect(self.subView.frame), NSStringFromCGRect(panel.frame));
    
    CGRect subRect = [self.subView frame];//最大的subviewframe
    CGRect selfRect = [self.view frame];
    subRect.size.height = panelRect.size.height;//高度和panel一样
    subRect.origin.y = (selfRect.size.height - subRect.size.height)/2;//顶点坐标在屏幕中心少下一点
    subView.frame = subRect;
    
    //根据 cancelOperation 决定是否显示右上角的取消按钮
    if (self.info.cancelOperation) {
        _cancelButtonOnCorner.hidden = NO;
    }else {
        _cancelButtonOnCorner.hidden = YES;
    }
    
    //NSLog(@"%s  sub :%@; panel :%@", __FUNCTION__, NSStringFromCGRect(self.subView.frame), NSStringFromCGRect(panel.frame));

}

#pragma mark---根据info更改不同状态 (备用)

-(void)changeByInfoDifferent
{
    if (tipsLabelt.text!=nil)
    {
        self.tipsLabelt.text = nil;
    }
    CGPoint beginPoint = {10,10};
    CGRect panelRect = [panel frame];
    CGRect frameforTipsLabelt = [self.tipsLabelt frame];
    CGRect frameforAcitive = [activity frame];//frameforActive ----滚轮的frame
    CGRect frameforLabel = [tipsLabel frame];
    int heightAcitiveWithTipLabel = 10;//label与滚轮的间隔
    /*****tiplabelt**********/
    if (tipsStringt.length !=0) {
        CGRect myRect = [tipsLabel frame];
        self.tipsLabelt = [[UILabel alloc] initWithFrame:myRect];//让两个label大小一样    
        self.tipsLabelt.font = [UIFont fontWithName:@"Arial" size:16];
        self.tipsLabelt.textAlignment = UITextAlignmentCenter;
        self.tipsLabelt.textColor = [UIColor whiteColor];
        self.tipsLabelt.backgroundColor = [UIColor clearColor];
        self.tipsLabelt.numberOfLines = 2;
        self.tipsLabelt.lineBreakMode = UILineBreakModeCharacterWrap;
        self.tipsLabelt.font = [UIFont fontWithName:@"Helvetica" size:14];
        self.tipsLabelt.text = info.title;
        //NSLog(@"the title is %@",info.title);
        frameforTipsLabelt.origin.x = beginPoint.x;
        frameforTipsLabelt.origin.y = beginPoint.y;
        frameforTipsLabelt.size.width = panelRect.size.width - 20;
        CGSize mychangelabelt = [tipsLabelt.text sizeWithFont:[UIFont fontWithName:@"Arial" size:16] constrainedToSize:CGSizeMake(panelRect.size.width-20,2000)lineBreakMode:UILineBreakModeCharacterWrap];
        frameforTipsLabelt.size.height = mychangelabelt.height;
        self.tipsLabelt.frame = frameforTipsLabelt;
        self.tipsLabelt.hidden = NO;
        [self.panel addSubview:tipsLabelt];
    }
      /*****activity**********/
    if (info.hidden == NO) {
        frameforAcitive.size.width = 20;
        frameforAcitive.size.height = 20;
        frameforAcitive.origin.x = (panelRect.size.width - frameforAcitive.size.width)/2;//让active居中在pannel中
        if (info.title.length != 0) {
          frameforAcitive.origin.y = tipsLabelt.frame.size.height + heightAcitiveWithTipLabel+tipsLabelt.frame.origin.y;
        }
        else {
            frameforAcitive.origin.y = heightAcitiveWithTipLabel;
        }

        activity.frame = frameforAcitive;
    }

    /**********tipslabel.frame*****************/
    CGSize mychangelabel = [tipsLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:16] constrainedToSize:CGSizeMake(panelRect.size.width-20,2000)lineBreakMode:UILineBreakModeCharacterWrap];    //文字动态的高度，固定的宽度
    frameforLabel.origin.x = beginPoint.x ;//相对panelview的左上顶点的位置
    
    if (info.hidden == YES&&tipsLabelt.text == nil)
    {
        frameforLabel.origin.y = heightAcitiveWithTipLabel;
    }
    else if (info.hidden == NO&&tipsLabelt.text == nil)
    {
        frameforLabel.origin.y = heightAcitiveWithTipLabel+frameforAcitive.origin.y+frameforAcitive.size.height;
    }
    else if(info.hidden == YES&&info.title.length != 0)
    {
        frameforLabel.origin.y = heightAcitiveWithTipLabel + frameforTipsLabelt.origin.y + frameforTipsLabelt.size.height; 
    }
    else if(info.hidden == NO){
        frameforLabel.origin.y= heightAcitiveWithTipLabel+frameforAcitive.size.height + frameforAcitive.origin.y;
    }
    frameforLabel.size.width = panelRect.size.width - 20;//tiplabel就要比下层的panel窄20
    frameforLabel.size.height = mychangelabel.height;//label的高度是文字的动态高度
    self.tipsLabel.frame = frameforLabel;
    /**********panel.frame*****************/////变化了
    if (info.hidden == NO&&info.title.length !=0) 
    {
        panelRect.size.height = frameforLabel.size.height + frameforLabel.origin.y + heightAcitiveWithTipLabel;
    }
   else  if (info.hidden == YES&&info.title.length !=0 )
    {
        panelRect.size.height = frameforLabel.size.height + frameforLabel.origin.y + heightAcitiveWithTipLabel;
    }
    else if (info.hidden == YES&&info.title.length ==0 ) 
    {
        panelRect.size.height = frameforLabel.size.height+frameforLabel.origin.y + heightAcitiveWithTipLabel ; 
    }
    else if (info.hidden == NO&&info.title.length ==0 ) 
    {
        panelRect.size.height = frameforLabel.size.height+frameforLabel.origin.y + heightAcitiveWithTipLabel ; 
    }
    panel.frame = panelRect;//
    /**********subview.frame*****************/
    CGRect subRect = [self.subView frame];//最大的subviewframe
    CGRect selfRect = [self.view frame];
    subRect.size.height = panelRect.size.height;//高度和panel一样
    subRect.origin.y = (selfRect.size.height - subRect.size.height)/2;//顶点坐标在屏幕中心少下一点
    subView.frame = subRect;
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.subView = nil;
    self.tipsString = nil;
    self.tipsLabel = nil;
    self.tipsLabelt = nil;
    self.view = nil;
    self.rightButton = nil;
    self.leftButton = nil;
    self.panel = nil;
    self.backgroundImage = nil;
    self.activity = nil;
    self.cancelButtonOnCorner = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#if !ARC_ENABLED
- (void)dealloc
{
    SafeRelease(textViewForTips);
    SafeRelease(subView);
    SafeRelease(panel);
    SafeRelease(rightButton);
    SafeRelease(activity);
    SafeRelease(tipsLabel);
    SafeRelease(tipsLabelt);
    SafeRelease(leftButton);
    SafeRelease(modal);
    SafeRelease(tipsString);
    SafeRelease(tipsStringt);
    SafeRelease(info);
    delegate = nil;
    SafeRelease(_cancelButtonOnCorner);
    
    [_tips_operation_queue cancelAllOperations];
    SafeRelease(_tips_operation_queue);
    [super dealloc];
}
#endif

- (void)showView
{
    [self showViewUntilSeconds:info.timeInterval];
}

- (void)showViewUntilSeconds:(NSTimeInterval)timeInterval
{
    if (self.view) {
    }

    self.modal.modalStyle = MODAL_FADE_IN;
    
    //dispatch_queue_t runOnQueue = _tips_queue;
    
    dispatch_async(dispatch_get_main_queue(), ^{
    
        //update ui elements
        self.tipsLabel.text = tipsString;
        self.activity.hidden = self.info.hidden;
        [self differentInfoToView];//去掉了以前的if、else
        
        if (self.info.updateView && !self.view.window) {
            //show on window
            [self.modal showModalView:self OnWidnow:_mainWindow];
        }else {
            [self.modal showModalView:self OnWidnow:_mainWindow];
        }

        //hide after a little delay
        [_tips_operation_queue addOperationWithBlock:^{
            if (timeInterval > 0) {
                [_tips_operation_queue cancelAllOperations];
                [[NSRunLoop currentRunLoop] cancelPerformSelector:@selector(hideModalView:) target:self.modal argument:self];
                [self.modal performSelector:@selector(hideModalView:) withObject:self afterDelay:timeInterval];
                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval + 0.5f]];
            }
        }];
        
        //Note:连续出现多个延时消失的提示框，会导致队列内的block无法完全运行
//        dispatch_async(runOnQueue, ^{
//            if (timeInterval > 0)
//            {
//                [[NSRunLoop currentRunLoop] cancelPerformSelector:@selector(hideModalView:) target:self.modal argument:self];
//                [self.modal performSelector:@selector(hideModalView:) withObject:self afterDelay:timeInterval];
//                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval + 0.5f]];
//            }
//        });
    
    });

  
}

- (void)showViewUntilSecondsNumber:(NSNumber *)timeInterval
{
    [self showViewUntilSeconds:timeInterval.doubleValue];
}

#pragma mark - modal view delegate

- (void)showModalEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    NSLog(@"%s; mainThread :%@;", __FUNCTION__, NSStringFromBool([NSThread isMainThread]));
    if (delegate && [delegate respondsToSelector:@selector(animationShownFinished:)]) {
        NSLog(@"%s  finished :%@; mainThread :%@;", __FUNCTION__, finished, NSStringFromBool([NSThread isMainThread]));
        [delegate animationShownFinished:finished.boolValue];
    }
}

- (void)hideModalEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    NSLog(@"%s; mainThread :%@;", __FUNCTION__, NSStringFromBool([NSThread isMainThread]));
    if (delegate && [delegate respondsToSelector:@selector(animationHiddenFinished:)]) {
        NSLog(@"%s  finished :%@; mainThread :%@;", __FUNCTION__, finished, NSStringFromBool([NSThread isMainThread]));
        [delegate animationHiddenFinished:finished.boolValue];
    }
    [self.view removeFromSuperview];
}

- (void)onClick_rightButton:(id)sender
{
    [self.modal hideModalView:self];
}

- (void)onClick_leftButton:(id)sender
{
    [self.modal hideModalView:self];
}

- (void)onClickButton:(UIButton *)button
{
    if (button == leftButton || button == rightButton) {
        [self hide];
    }else if (button == _cancelButtonOnCorner) {
        if (self.info.cancelOperation) {
            [self.info cancelOperation];
        }
        //Keep cancelBlock only run once.
        self.info.cancelOperation = NULL;
        [self hide];
    }
}

- (void)showViewWithTipsInfo:(TDTipsInfo *)aInfo
{
    if (self.view) {
    }

    if (aInfo && !aInfo.message && !aInfo.title) {
        [self hide];
        return;
    }
    
    //run on background queue
    dispatch_async(dispatch_get_main_queue(), ^{
        //config data
        self.info = aInfo;
        self.tipsString = self.info.message;
        self.tipsStringt = self.info.title;
        //show
        [self showViewUntilSeconds:info.timeInterval];
    });

    //Note:Keep tips shown on current thread at least 0.3 secs. 2012.12.9 Deheng.Xu
    NSDate *_dateStart = [NSDate date];
    while ([[NSDate date] timeIntervalSinceDate:_dateStart] < 0.3f) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
    }

}

- (void)hide
{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.activity.hidden = YES;
        [self.modal hideModalView:self];
    });
    
    //Todo:待清理
    /*
     self.activity.hidden = YES;
     [self.modal performSelectorOnMainThread:@selector(hideModalView:) withObject:self waitUntilDone:NO];
     */
}


@end

#pragma mark - TDTipsInfo implementation

@implementation TDTipsInfo

@synthesize title;
@synthesize message;
@synthesize hidden;
@synthesize timeInterval;
@synthesize cancelOperation;

+ (TDTipsInfo *)infoWithMessage:(NSString *)message TimeInterval:(CGFloat)timeInterval HiddenActivity:(BOOL)hidden
{
    TDTipsInfo * info = [TDTipsInfo new];
    info.message = message;
    info.timeInterval = timeInterval;
    info.hidden = hidden;
    info.title = nil;
    info.cancelOperation = NULL;
    info.updateView = NO;
    return XAutorelease(info);
}

+ (TDTipsInfo *)infoWithMessage:(NSString *)message TimeInterval:(CGFloat)timeInterval HiddenActivity:(BOOL)hidden NeedUpdateView:(BOOL)needUpdate
{
    TDTipsInfo *info = [self infoWithMessage:message TimeInterval:timeInterval HiddenActivity:hidden];
    info.updateView = needUpdate;
    return info;
}

+ (TDTipsInfo *)infoWithMessage:(NSString *)message TimeInterval:(CGFloat)timeInterval HiddenActivity:(BOOL)hidden UsingCancelBlock:(void (^)())cancelOperation
{
    TDTipsInfo *info = [self infoWithMessage:message TimeInterval:timeInterval HiddenActivity:hidden];
    info.cancelOperation = cancelOperation;
    return info;
}

+ (TDTipsInfo *)infoWithTitle:(NSString*)title Message:(NSString *)message TimeInterval:(CGFloat)timeInterval HiddenActivity:(BOOL)hidden
{
    TDTipsInfo * info = [self infoWithMessage:message TimeInterval:timeInterval HiddenActivity:hidden];//[TDTipsInfo new];
    info.title = title;
//    info.message = message;
//    info.timeInterval = timeInterval;
//    info.hidden = hidden;
//    info.cancelOperation = NULL;
//    info.updateView = NO;
    return info;
}

#if !ARC_ENABLED
- (void)dealloc
{
#if DEBUG
    NSLog(@"%s", __FUNCTION__);
#endif
    SafeRelease(title);
    SafeRelease(message);
    SafeReleaseBlock(cancelOperation);
    [super dealloc];
}
#endif

@end

#endif
